package com.koron.css2.serviceManage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.TService;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.TServiceDetailMapper;
import com.koron.css2.serviceManage.mapper.TServiceMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;

public class ServiceQuery implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		@SuppressWarnings("rawtypes")
		MessageBean<TService> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", TService.class);

		try {
			HashMap<String,String> map = JsonUtils.objectToPojo(req.getData(), HashMap.class);
			TServiceMapper mapper = factory.getMapper(TServiceMapper.class);
			TServiceDetailMapper detailMapper = factory.getMapper(TServiceDetailMapper.class);
			TService tService = mapper.selectByPrimaryKey(map.get("serviceId"));
			tService.settServiceDetails(detailMapper.findServiceDetailByServiceCode(tService.getServiceCode()));
			info.setData(tService);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
